package com.flying.pas.security.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.Transaction;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.security.Principal;
import com.flying.framework.service.AbstractService;
import com.flying.pas.security.model.RoleUser;

@Service(value="RoleUserService", desc="")
public class RoleUserService extends AbstractService{
        
	@MethodInfo("新增")
	@DaoCreate(entity=RoleUser.ENTITY_NAME)
	public RoleUser create(
			@Param(value=RoleUser.USER_ID, required=true,	desc="用户ID") Long user_id,
			@Param(value=RoleUser.ROLE_ID, required=true,	desc="角色ID") Long role_id,
			@Param(value=RoleUser.CREATOR, required=false,	desc="创建人") String creator,
			@Param(value=RoleUser.CREATE_TIME, required=false,	desc="创建时间") Date create_time
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(RoleUser.class,
				RoleUser.CREATOR, p == null?null: p.getName(),
				RoleUser.CREATE_TIME, new Date());
	}

	@MethodInfo("删除")
	@DaoRemove(entity=RoleUser.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=RoleUser.ROLE_ID, required=true,	desc="角色ID") Long role_id,
			@Param(value=RoleUser.USER_ID, required=true,	desc="用户ID") Long user_id
		) throws Exception {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=RoleUser.ENTITY_NAME)
	public ActionResult removeByUser(
			@Param(value=RoleUser.USER_ID, required=true,	desc="用户ID") Long user_id
		) throws Exception {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=RoleUser.ENTITY_NAME)
	public ActionResult removeByRole(
			@Param(value=RoleUser.ROLE_ID, required=true,	desc="角色ID") Long role_id
		) throws Exception {
		return null;
	}

	@MethodInfo("设置用户角色")
	@Transaction
	public ActionResult updateUserRoles(
			@Param(value=RoleUser.USER_ID, required=true,	desc="用户ID") Long user_id,
			@Param(value=RoleUser.ROLE_ID, required=true,	desc="角色ID") Long[] role_ids
		) throws Exception {
		this.removeByUser(user_id);
		for(long role_id: role_ids) {
			this.create(user_id, role_id, null, null);
		}
		return ModelFactory.create(ActionResult.class,
				Codes.CODE, Codes.SUCCESS,
				Codes.EFFECT_ROWS, role_ids.length);
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=RoleUser.ENTITY_NAME, pageable=false, modelClass=RoleUser.class)
	public QueryResult<RoleUser> findByUserId(
			@Param(value=RoleUser.USER_ID, required=true,	desc="用户ID") Long user_id) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=RoleUser.ENTITY_NAME, pageable=true, modelClass=RoleUser.class)
	public QueryResult<RoleUser> findByRole(
			@Param(value=RoleUser.ROLE_ID, required=true,	desc="角色ID") Long role_id,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=RoleUser.ENTITY_NAME, pageable=true, modelClass=RoleUser.class)
	public QueryResult<RoleUser> findAll(
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=RoleUser.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public RoleUser findById(
			@Param(value=RoleUser.ROLE_ID, required=true,	desc="角色ID") Long role_id,
			@Param(value=RoleUser.USER_ID, required=true,	desc="用户ID") Long user_id
		) throws Exception {
		return null;
	}

}